package com.lyp.supermanage.view;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.blankj.utilcode.util.ToastUtils;
import com.lyp.supermanage.R;
import com.lyp.supermanage.adapter.DetailsAdapter;
import com.lyp.supermanage.api.Api;
import com.lyp.supermanage.bean.DetailsBean;
import com.lyp.supermanage.bean.StatementBean;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class DetailsActivity extends Activity{

    private RecyclerView mRecyclerView;
    private DetailsAdapter mAdapter;
    private LinearLayoutManager mLinearLayoutManager;
    private List<DetailsBean.DataBean> mList = new ArrayList<>();
    private ImageView mBack;
    private TextView mTitle;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_car);
        init();
    }

    public void init() {
        mTitle = (TextView) findViewById(R.id.tv_title);
        mBack = (ImageView) findViewById(R.id.iv_back);
        mRecyclerView = (RecyclerView) findViewById(R.id.grid_recycler);
        mLinearLayoutManager = new LinearLayoutManager(this);
        mRecyclerView.setLayoutManager(mLinearLayoutManager);
        mAdapter = new DetailsAdapter(mList);
        // 实例化 RecyclerView Adapter
        mRecyclerView.setAdapter(mAdapter);
        mAdapter.setOnItemClickListener(new DetailsAdapter.MyItemClickListener() {
            @Override
            public void onItemClick(View v, int position) {
                Object dataBean = getIntent().getExtras().get("databean");
                if (dataBean instanceof StatementBean.DataBean.CarBean) {
                    Intent intent = new Intent(DetailsActivity.this,ReportDetailsActivity.class);
                    Bundle bundle = new Bundle();
                    bundle.putSerializable("databean", mList.get(position));
                    intent.putExtras(bundle);
                    startActivity(intent);
                } else if (dataBean instanceof StatementBean.DataBean.DetectionBean) {
                    Intent intent = new Intent(DetailsActivity.this,ReportDetailsActivity.class);
                    Bundle bundle = new Bundle();
                    bundle.putSerializable("databean", mList.get(position));
                    intent.putExtras(bundle);
                    startActivity(intent);
                } else if (dataBean instanceof StatementBean.DataBean.GovernBean){
                    Intent intent = new Intent(DetailsActivity.this,ReportDetailsActivity.class);
                    Bundle bundle = new Bundle();
                    bundle.putSerializable("databean", mList.get(position));
                    intent.putExtras(bundle);
                    startActivity(intent);
                }

                }
        });

        Object dataBean = getIntent().getExtras().get("databean");
        if (dataBean instanceof StatementBean.DataBean.CarBean) {
            mTitle.setText("车内空气净化服务");
            details(((StatementBean.DataBean.CarBean) dataBean).getType());
        } else if (dataBean instanceof StatementBean.DataBean.DetectionBean) {
            mTitle.setText("第三方检测服务");
            details(((StatementBean.DataBean.DetectionBean) dataBean).getType());
        } else if (dataBean instanceof StatementBean.DataBean.GovernBean){
            mTitle.setText("室内空气净化服务");
            details(((StatementBean.DataBean.GovernBean) dataBean).getType());
        }

        mBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    //详情
    public void details(String type) {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(Api.APP_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        Api api = retrofit.create(Api.class);
        Call<DetailsBean> call = api.getDetails(type);
        call.enqueue(new Callback<DetailsBean>() {
            @Override
            public void onResponse(Call<DetailsBean> call, final Response<DetailsBean> response) {
                if (response.body().getResult() == 1 ) {
                    DetailsActivity.this.runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            mList.addAll(response.body().getData());
                            mAdapter.notifyDataSetChanged();
                        }
                    });
                } else{
                    ToastUtils.showShort(response.body().getMessage());
                }
            }
            @Override
            public void onFailure(Call<DetailsBean> call, Throwable t) {
                ToastUtils.showShort(t.getMessage());
            }
        });
    }

}
